﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using System.Web.Security;
using ZLCMS.Business.Commands.SessionCommands;
using DotNet.Common.Command;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using NHibernate.Criterion;

namespace ZLCMS.Web.MemberUser
{
    public partial class ResetPassword : System.Web.UI.Page
    {
        EmailCheckInfo _checkInfo;
        public string _uid;
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScriptManager csm = this.Page.ClientScript;
            if(!IsPostBack)
            {
                hid_uid.Value = Request["uid"];
            }
            _uid = hid_uid.Value;
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                if (Request["type"] == "emailresetpassword")
                {

                    _checkInfo = session.CreateCriteria<EmailCheckInfo>().Add(Expression.Eq("CheckString", Request["checkstring"])).Add(Expression.Eq("MemberAccount", _uid)).Add(Expression.Eq("Status", "0")).UniqueResult<EmailCheckInfo>();
                }
                else
                {
                    _checkInfo = session.CreateCriteria<EmailCheckInfo>().Add(Expression.Eq("MemberAccount", _uid)).Add(Expression.Eq("Status", "0")).UniqueResult<EmailCheckInfo>();
                }
            }
            if (_checkInfo == null)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('链接无效!');location.href='/MemberUser/Login.aspx'", true);
            }
            else
            {
                if (Request["checkstring"] != _checkInfo.CheckString && Request["type"] == "emailresetpassword")
                {
                    csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('链接无效!');location.href='/MemberUser/Login.aspx'", true);
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Request["type"] == "emailresetpassword")
            {
                EmailSetPassword();
            }
            else if (Request["type"] == "phoneresetpassword")
            {
                PhoneSetPassword();
            }
           
        }

        private void PhoneSetPassword()
        {
            ClientScriptManager csm = this.Page.ClientScript;

            if (tb_new1.Text != tb_new2.Text)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('两次输入密码不匹配。');", true);
                return;
            }
            MemberInfo member;
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                member = session.CreateQuery("from MemberInfo where Telephone=:phone").SetParameter("phone", _uid).UniqueResult<MemberInfo>();
                EmailCheckInfo _checkInfo = session.CreateCriteria<EmailCheckInfo>().Add(Expression.Eq("CheckString", txt_code.Text.Trim())).Add(Expression.Eq("MemberAccount", _uid)).Add(Expression.Eq("Status", "0")).UniqueResult<EmailCheckInfo>();
                if (member == null)
                {
                    csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('账户无效。');", true);
                    return;
                }
                if (_checkInfo != null)//判断手机验证码是否正确
                {
                     ChangePasswordCommandParameter cmdParam = new ChangePasswordCommandParameter();

                cmdParam.UserCode = member.AccountId;
                cmdParam.OldPassword = member.Password;
                cmdParam.NewPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(tb_new1.Text.Trim(), "MD5").ToLower();

                CommandResult result = new ChangePasswordCommand().Execute(cmdParam) as CommandResult;

                if (result.Error)
                {
                    csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('" + result.ErrorMsg + "');", true);
                    return;
                }
                _checkInfo.Status = "1";
                session.Update(_checkInfo);
                session.Flush();
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('操作成功。');window.location.href='/MemberUser/Default.aspx'", true);
                }
                else
                {
                    csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('验证码无效!');", true);
                    return;
                }
            }
        }

        protected void EmailSetPassword()
        {
            ClientScriptManager csm = this.Page.ClientScript;


            if (tb_new1.Text != tb_new2.Text)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('两次输入密码不匹配。');", true);
                return;
            }
            MemberInfo member;
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                member = session.CreateQuery("from MemberInfo where AccountId=:acc").SetParameter("acc", Request["uid"]).UniqueResult<MemberInfo>();
       
            if (member == null)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('账户无效。');", true);
            }

            ChangePasswordCommandParameter cmdParam = new ChangePasswordCommandParameter();

            cmdParam.UserCode = member.AccountId;
            cmdParam.OldPassword = member.Password;
            cmdParam.NewPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(tb_new1.Text.Trim(), "MD5").ToLower();

            CommandResult result = new ChangePasswordCommand().Execute(cmdParam) as CommandResult;

                if (result.Error)
                {
                    csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('" + result.ErrorMsg + "');", true);
                    return;
                }
                _checkInfo.Status = "1";
                session.Update(_checkInfo);
                session.Flush();
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('操作成功。');window.location.href='/MemberUser/Default.aspx'", true);
            }
        }
    }
}
